package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class ServeClient {

    @Resource
    private ServeApi serveApi;
    /**
     * 定义一个方法，通过这个方法 调用Feign客户端接口发起远程调用
     * 并给方法添加@SentinelResource，配置降级逻辑
     *  value：Sentinel资源的标识,也就是正常时的情况
     *  fallback：非熔断、非限流的异常的降级方法名
     *  blockHandler：熔断、限流异常的降级方法名
     */
    @SentinelResource(value = "findServeById", fallback = "findServeByIdFallback", blockHandler = "findServeByIdBlockHandler")
    public ServeAggregationResDTO findServeById(Long id) {
        log.info("根据id查询地址薄：{}", id);
        return serveApi.findById(id);
    }

    public ServeAggregationResDTO findServeByIdFallback(Long id, Throwable t){
        log.error("非限流、熔断等导致的异常执行的降级方法，id:{},throwable:", id, t);
        return null;
    }

    public ServeAggregationResDTO findServeByIdBlockHandler(Long id, BlockException e) {
        log.error("触发限流、熔断时执行的降级方法，id:{},blockException:", id, e);
        return null;
    }
}